﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetExtensions.AspNet
{
    public class ValueCollection<T> : ICollection<T> where T : class
    {
        public ValueCollection()
        {

        }
        public ValueCollection(List<T> values)
        {
            if (values != null) this.Values = values;
        }
        protected readonly List<T> Values = new List<T>();
        public int Count => Values.Count;
        public bool IsReadOnly => false;

        public T this[int i]
        {
            get { return Values[i]; }
            set { Values[i] = value; }
        }

        public virtual void Add(T item)
        {
            Values.Add(item);
        }
        public virtual void AddRange(IEnumerable<T> items)
        {
            Values.AddRange(items);
        }
        public virtual void AddRange(IEnumerable<T> items, Func<T, bool> where)
        {
            foreach (var item in items) this.Add(item, where);
        }
        public virtual void Add(T item, Func<T, bool> where)
        {
            T oldest = Values.FirstOrDefault(where);
            if (oldest == null)
                Values.Add(item);
            else
            {
                int index = Values.IndexOf(oldest);
                Values[index] = item;
            }
        }
        public virtual bool TryGet(Func<T, bool> where, out T value)
        {
            value = Values.FirstOrDefault(where);
            return value != null;
        }

        public void Clear()
        {
            Values.Clear();
        }

        public bool Contains(T item)
        {
            return Values.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            Values.CopyTo(array, arrayIndex);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return Values.GetEnumerator();
        }

        public bool Remove(T item)
        {
            return Values.Remove(item);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Values.GetEnumerator();
        }
    }
    public class KeyValueCollection : ValueCollection<KeyValue>
    {
        public KeyValueCollection() : base()
        {

        }
        public KeyValueCollection(List<KeyValue> values) : base(values)
        {

        }
        public override void Add(KeyValue item)
        {
            base.Add(item, x => x.Name == item.Name);
        }
    }

    public class KeyValue
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public string Path { get; set; }
        public string Domain { get; set; }

        public bool IsEmpty()
        {
            return string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Value);
        }

        public KeyValue()
        {

        }

        public KeyValue(string name, string value)
        {
            Name = name;
            Value = value;
        }
        public KeyValue(string name, string value, string path)
        {
            Name = name;
            Value = value;
            Path = path;
        }

        public KeyValue(string name, string value, string path, string domain)
        {
            Name = name;
            Value = value;
            Path = path;
            Domain = domain;
        }
    }
}
